home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / pyxmpp / clientstream.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  234 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __revision__ = '$Id: clientstream.py 652 2006-08-27 19:41:15Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import logging
  7. from pyxmpp.stream import Stream
  8. from pyxmpp.streambase import BIND_NS
  9. from pyxmpp.streamsasl import SASLNotAvailable, SASLMechanismNotAvailable
  10. from pyxmpp.jid import JID
  11. from pyxmpp.utils import to_utf8
  12. from pyxmpp.exceptions import StreamError, StreamAuthenticationError, FatalStreamError
  13. from pyxmpp.exceptions import ClientStreamError, FatalClientStreamError
  14.  
  15. class ClientStream(Stream):
  16.     
  17.     def __init__(self, jid, password = None, server = None, port = None, auth_methods = ('sasl:DIGEST-MD5',), tls_settings = None, keepalive = 0, owner = None):
  18.         sasl_mechanisms = []
  19.         for m in auth_methods:
  20.             if not m.startswith('sasl:'):
  21.                 continue
  22.             
  23.             m = m[5:].upper()
  24.             sasl_mechanisms.append(m)
  25.         
  26.         Stream.__init__(self, 'jabber:client', sasl_mechanisms = sasl_mechanisms, tls_settings = tls_settings, keepalive = keepalive, owner = owner)
  27.         self.server = server
  28.         self.port = port
  29.         self.password = password
  30.         self.auth_methods = auth_methods
  31.         self.my_jid = jid
  32.         self.me = None
  33.         self._auth_methods_left = None
  34.         self._ClientStream__logger = logging.getLogger('pyxmpp.ClientStream')
  35.  
  36.     
  37.     def _reset(self):
  38.         Stream._reset(self)
  39.         self._auth_methods_left = []
  40.  
  41.     
  42.     def connect(self, server = None, port = None):
  43.         self.lock.acquire()
  44.         
  45.         try:
  46.             self._connect(server, port)
  47.         finally:
  48.             self.lock.release()
  49.  
  50.  
  51.     
  52.     def _connect(self, server = None, port = None):
  53.         if not (self.my_jid.node) or not (self.my_jid.resource):
  54.             raise ClientStreamError, 'Client JID must have username and resource'
  55.         
  56.         if not server:
  57.             server = self.server
  58.         
  59.         if not port:
  60.             port = self.port
  61.         
  62.         if server:
  63.             self._ClientStream__logger.debug('server: %r', (server,))
  64.             service = None
  65.         else:
  66.             service = 'xmpp-client'
  67.         if port is None:
  68.             port = 5222
  69.         
  70.         if server is None:
  71.             server = self.my_jid.domain
  72.         
  73.         self.me = self.my_jid
  74.         Stream._connect(self, server, port, service, self.my_jid.domain)
  75.  
  76.     
  77.     def accept(self, sock):
  78.         Stream.accept(self, sock, self.my_jid)
  79.  
  80.     
  81.     def _post_connect(self):
  82.         if self.initiator:
  83.             self._auth_methods_left = list(self.auth_methods)
  84.             self._try_auth()
  85.         
  86.  
  87.     
  88.     def _try_auth(self):
  89.         if self.authenticated:
  90.             self._ClientStream__logger.debug('try_auth: already authenticated')
  91.             return None
  92.         
  93.         self._ClientStream__logger.debug('trying auth: %r', (self._auth_methods_left,))
  94.         if not self._auth_methods_left:
  95.             raise StreamAuthenticationError, 'No allowed authentication methods available'
  96.         
  97.         method = self._auth_methods_left[0]
  98.         if method.startswith('sasl:'):
  99.             if self.version:
  100.                 self._auth_methods_left.pop(0)
  101.                 
  102.                 try:
  103.                     self._sasl_authenticate(self.my_jid.node, None, mechanism = method[5:].upper())
  104.                 except (SASLMechanismNotAvailable, SASLNotAvailable):
  105.                     self._ClientStream__logger.debug('Skipping unavailable auth method: %s', (method,))
  106.                     return self._try_auth()
  107.                 except:
  108.                     None<EXCEPTION MATCH>(SASLMechanismNotAvailable, SASLNotAvailable)
  109.                 
  110.  
  111.             None<EXCEPTION MATCH>(SASLMechanismNotAvailable, SASLNotAvailable)
  112.             self._auth_methods_left.pop(0)
  113.             self._ClientStream__logger.debug('Skipping auth method %s as legacy protocol is in use', (method,))
  114.             return self._try_auth()
  115.         else:
  116.             self._auth_methods_left.pop(0)
  117.             self._ClientStream__logger.debug('Skipping unknown auth method: %s', method)
  118.             return self._try_auth()
  119.  
  120.     
  121.     def _get_stream_features(self):
  122.         features = Stream._get_stream_features(self)
  123.         if self.peer_authenticated:
  124.             bind = features.newChild(None, 'bind', None)
  125.             ns = bind.newNs(BIND_NS, None)
  126.             bind.setNs(ns)
  127.             self.set_iq_set_handler('bind', BIND_NS, self.do_bind)
  128.         
  129.         return features
  130.  
  131.     
  132.     def do_bind(self, stanza):
  133.         fr = stanza.get_from()
  134.         if fr and fr != self.peer:
  135.             r = stanza.make_error_response('forbidden')
  136.             self.send(r)
  137.             r.free()
  138.             return None
  139.         
  140.         resource_n = stanza.xpath_eval('bind:bind/bind:resource', {
  141.             'bind': BIND_NS })
  142.         if resource_n:
  143.             resource = resource_n[0].getContent()
  144.         else:
  145.             resource = 'auto'
  146.         if not resource:
  147.             r = stanza.make_error_response('bad-request')
  148.         else:
  149.             self.unset_iq_set_handler('bind', BIND_NS)
  150.             r = stanza.make_result_response()
  151.             self.peer.set_resource(resource)
  152.             q = r.new_query(BIND_NS, 'bind')
  153.             q.newTextChild(None, 'jid', to_utf8(self.peer.as_unicode()))
  154.             self.state_change('authorized', self.peer)
  155.         r.set_to(None)
  156.         self.send(r)
  157.         r.free()
  158.  
  159.     
  160.     def get_password(self, username, realm = None, acceptable_formats = ('plain',)):
  161.         _unused = realm
  162.         if self.initiator and self.my_jid.node == username and 'plain' in acceptable_formats:
  163.             return (self.password, 'plain')
  164.         else:
  165.             return (None, None)
  166.  
  167.     
  168.     def get_realms(self):
  169.         return [
  170.             self.my_jid.domain]
  171.  
  172.     
  173.     def choose_realm(self, realm_list):
  174.         if not realm_list:
  175.             return self.my_jid.domain
  176.         
  177.         if self.my_jid.domain in realm_list:
  178.             return self.my_jid.domain
  179.         
  180.         return realm_list[0]
  181.  
  182.     
  183.     def check_authzid(self, authzid, extra_info = None):
  184.         if not extra_info:
  185.             extra_info = { }
  186.         
  187.         if not authzid:
  188.             return 1
  189.         
  190.         if not self.initiator:
  191.             jid = JID(authzid)
  192.             if not extra_info.has_key('username'):
  193.                 ret = 0
  194.             elif jid.node != extra_info['username']:
  195.                 ret = 0
  196.             elif jid.domain != self.my_jid.domain:
  197.                 ret = 0
  198.             elif not jid.resource:
  199.                 ret = 0
  200.             else:
  201.                 ret = 1
  202.         else:
  203.             ret = 0
  204.         return ret
  205.  
  206.     
  207.     def get_serv_type(self):
  208.         return 'xmpp'
  209.  
  210.     
  211.     def get_serv_name(self):
  212.         return self.my_jid.domain
  213.  
  214.     
  215.     def get_serv_host(self):
  216.         return self.my_jid.domain
  217.  
  218.     
  219.     def fix_out_stanza(self, stanza):
  220.         if self.initiator:
  221.             stanza.set_from(None)
  222.         elif not stanza.get_from():
  223.             stanza.set_from(self.my_jid)
  224.         
  225.  
  226.     
  227.     def fix_in_stanza(self, stanza):
  228.         if self.initiator:
  229.             Stream.fix_in_stanza(self, stanza)
  230.         else:
  231.             stanza.set_from(self.peer)
  232.  
  233.  
  234.